package com.aos.erp.message.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.message.enums.DelStatus;
import com.aos.erp.message.mapper.MessageConfigMapper;
import com.aos.erp.message.model.MessageConfigInfo;
import com.aos.erp.message.model.vo.MessageConfigVo;
import com.aos.erp.message.service.MessageConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class MessageConfigServiceImpl extends ServiceImpl<MessageConfigMapper,MessageConfigInfo> implements MessageConfigService {

    @Override
    public MessageConfigInfo getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void saveConfig(MessageConfigVo dataInfo) throws BizException {

        checkSaveConfigData(dataInfo);

        // 保存消息配置项
        saveMessageConfigData(dataInfo);
    }

    @Override
    public void updateConfig(MessageConfigVo dataInfo) throws BizException {

        checkUpdateConfigData(dataInfo);

        // 保存消息配置
        baseMapper.updateConfig(dataInfo);
    }

    @Override
    public void deleteConfig(long dataId) throws BizException {

        if(dataId == 0) {
            throw new BizException("配置项ID不能为空");
        }
        baseMapper.deleteConfig(dataId);
    }

    @Override
    public PageBean<MessageConfigInfo> queryByPage(long pageIndex, long pageSize) {

        IPageBean<MessageConfigInfo> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(MessageConfigInfo.class);
        condition.eqFiled("isDel", DelStatus.del_no);
        condition.orderByDesc("id");

        return (PageBean<MessageConfigInfo>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }

    /**
     * 保存通知信息
     * @param dataInfo
     * @return
     */
    private long saveMessageConfigData(MessageConfigVo dataInfo) {

        Date nowDate = new Date();
        MessageConfigInfo messageConfigData = new MessageConfigInfo();
        BeanUtils.copyProperties(dataInfo, messageConfigData);
        messageConfigData.setIsDel(DelStatus.del_no);
        messageConfigData.setCreateUser(dataInfo.getOptUser());
        messageConfigData.setCreateTime(nowDate);
        messageConfigData.setUpdateUser(dataInfo.getOptUser());
        messageConfigData.setUpdateTime(nowDate);
        this.save(messageConfigData);
        return messageConfigData.getId();
    }

    private MessageConfigInfo getMessageConfigDetailByKey(String mtKey) {
        Condition condition = new Condition(MessageConfigInfo.class);
        condition.eqFiled("mcKey", mtKey);
        condition.eqFiled("isDel", DelStatus.del_no);
        return baseMapper.selectOne(condition.getWrapper());
    }

    private void checkConfigData(MessageConfigVo params) throws BizException {
        if(params.getMcKey().isEmpty()) {
            throw new BizException("配置项Key不能为空");
        }
        if(params.getMcValue().isEmpty()) {
            throw new BizException("配置项值不能为空");
        }

    }

    private void checkSaveConfigData(MessageConfigVo params) throws BizException {
        checkConfigData(params);
        MessageConfigInfo mcData = getMessageConfigDetailByKey(params.getMcKey());
        if(mcData != null && mcData.getId() > 0) {
            throw new BizException("该配置项已使用");
        }
    }

    private void checkUpdateConfigData(MessageConfigVo params) throws BizException {
        if(params.getId() == 0) {
            throw new BizException("配置项不能为空");
        }
        checkSaveConfigData(params);
        MessageConfigInfo mcData = getMessageConfigDetailByKey(params.getMcKey());
        if(mcData != null && mcData.getId() != params.getId()) {
            throw new BizException("该配置项已被使用");
        }
    }
}